#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
随访记录数据模型 - 完全符合技术文档B第5章标准v2.0
简洁设计，去除重复字段，专注核心随访功能
"""

from datetime import datetime, date, time
from backend.infrastructure.database import db

class FollowupRecord(db.Model):
    """随访记录模型 - 完全符合技术文档B第5章标准v2.0"""
    __tablename__ = "followup_records"
    
    # ✅基础字段（技术文档B第5.1.1节）
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='记录ID')
    student_id = db.Column(db.Integer, db.ForeignKey("students.id", ondelete="CASCADE"), 
                          nullable=False, index=True, comment='学生ID')
    
    # ✅随访基本信息（技术文档B第5.1.1节）
    followup_date = db.Column(db.Date, nullable=False, default=date.today, comment="随访日期")
    followup_time = db.Column(db.Time, nullable=True, comment="随访时间")
    interviewer = db.Column(db.String(50), nullable=False, comment="随访员姓名")
    interviewee = db.Column(db.String(50), nullable=False, comment="被访者姓名")
    interviewee_relation = db.Column(db.String(20), nullable=False, comment="被访者与学生关系")
    contact_method = db.Column(db.String(20), nullable=False, comment="联系方式")
    followup_duration = db.Column(db.Integer, nullable=True, comment="随访时长（分钟）")
    followup_type = db.Column(db.String(20), nullable=False, comment="随访类型")
    
    # ✅干预治疗评价（技术文档B第5.1.2节）
    intervention_satisfaction = db.Column(db.String(20), nullable=True, comment="干预治疗满意度")
    intervention_effect_evaluation = db.Column(db.String(20), nullable=True, comment="干预效果评价")
    intervention_compliance = db.Column(db.String(20), nullable=True, comment="干预依从性")
    side_effects = db.Column(db.String(200), nullable=True, comment="不良反应情况")
    
    # ✅视力变化情况（技术文档B第5.1.3节）
    vision_change_perception = db.Column(db.String(20), nullable=True, comment="家长感知的视力变化")
    vision_improvement_details = db.Column(db.Text, nullable=True, comment="视力改善具体表现")
    vision_concern_areas = db.Column(db.Text, nullable=True, comment="家长关心的视力问题")
    
    # ✅用眼情况评估（技术文档B第5.1.4节）
    daily_eye_use_time = db.Column(db.String(50), nullable=True, comment="每日用眼时间")
    
    # ✅随访内容记录（技术文档B第5.1.5节）
    content = db.Column(db.Text, nullable=True, comment="随访内容详情")
    feedback = db.Column(db.Text, nullable=True, comment="反馈信息")
    
    # ✅系统管理字段（技术文档B第5.1.6节）
    data_year = db.Column(db.String(4), nullable=True, comment="数据年份")
    created_at = db.Column(db.DateTime, default=datetime.utcnow, comment="创建时间")
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, comment="更新时间")
    
    # ✅关系定义（技术文档B第5.1.7节）
    student = db.relationship("Student", back_populates="followup_records")
    
    # ✅索引和约束（技术文档B第5.1.8节）
    __table_args__ = (
        # 防重复索引
        db.Index('ix_student_followup_date', 'student_id', 'followup_date'),
        # 随访员查询索引
        db.Index('ix_interviewer_date', 'interviewer', 'followup_date'),
        # 数据年份索引
        db.Index('ix_followup_data_year', 'data_year'),
        {'comment': '随访记录表v2.0 - 完全符合技术文档B第5章标准，简洁高效'}
    )
    
    def to_dict(self):
        """转换为字典格式 - v2.0标准版本（纯净版）"""
        return {
            # 基础字段
            "id": self.id,
            "student_id": self.student_id,
            
            # 随访基本信息
            "followup_date": self.followup_date.isoformat() if self.followup_date else "",
            "followup_time": self.followup_time.strftime("%H:%M") if self.followup_time else "",
            "interviewer": self.interviewer or "",
            "interviewee": self.interviewee or "",
            "interviewee_relation": self.interviewee_relation or "",
            "contact_method": self.contact_method or "",
            "followup_duration": self.followup_duration,
            "followup_type": self.followup_type or "",
            
            # 干预治疗评价
            "intervention_satisfaction": self.intervention_satisfaction or "",
            "intervention_effect_evaluation": self.intervention_effect_evaluation or "",
            "intervention_compliance": self.intervention_compliance or "",
            "side_effects": self.side_effects or "",
            
            # 视力变化情况
            "vision_change_perception": self.vision_change_perception or "",
            "vision_improvement_details": self.vision_improvement_details or "",
            "vision_concern_areas": self.vision_concern_areas or "",
            
            # 用眼情况评估
            "daily_eye_use_time": self.daily_eye_use_time or "",
            
            # 随访内容记录
            "content": self.content or "",
            "feedback": self.feedback or "",
            
            # 系统管理字段
            "data_year": self.data_year or "",
            "created_at": self.created_at.isoformat() if self.created_at else "",
            "updated_at": self.updated_at.isoformat() if self.updated_at else ""
        }
    
    def __repr__(self):
        return f'<FollowupRecord {self.id}: Student{self.student_id} - {self.followup_type} on {self.followup_date} by {self.interviewer}>'
